﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Tools;
using Domain;
using Domain.Entity;
using QQZoneMarketingDao;
using Spider.Spiders;
using Spider.Visited;

namespace Spider.Service
{
    /// <summary>
    /// 爬虫模块入口服务
    /// </summary>
    public static partial class SpiderService
    {
        /// <summary>
        /// 添加Url到ToDo队列,重做任务时
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        internal static bool AddReDoUrlToTodoQueue(string url)
        {
            DicVisited visited = new DicVisited();
            try
            {

                if (!string.IsNullOrWhiteSpace(url))
                {
                    ToDoData urData = new ToDoData();
                    urData.PageUrl = url;
                    var vis = visited.GetUrl(url);
                    if (vis != null)
                    {
                        urData.VisitedCount = vis.VisitedCount;
                    }
                    else
                    {
                        urData.VisitedCount = 1;
                    }
                    urData.DataSource = Spider.Spiders.Spider.GetPageDataSource(url);
                    ToDo.AddUrl(urData);
                }


                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("添加Url到ToDo队列出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 设置访问记录表中Url为已经访问成功
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        internal static bool SetVisitedUrlSuccess(string url)
        {
            DicVisited visited = new DicVisited();
            try
            {
                var data = visited.GetUrl(url);
                if (data != null)
                {
                    data.IsSucce = true;
                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog(" 设置访问记录表中Url为已经访问成功出错", ex);
                return false;
            }

        }
        /// <summary>
        /// 设置Url任务的当前状态
        /// </summary>
        /// <param name="url"></param>
        /// <param name="state"></param>
        internal static void SetUrlTaskState(string url, string state)
        {
            try
            {
                SpiderMonitor.SetUrlTaskState(url, state);
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("设置Url任务的当前状态出错", ex);
            }

        }
        /// <summary>
        /// 获取QQ空间启用了的账号
        /// </summary>
        /// <returns></returns>
        internal static List<QQAcount> GetEnabledQqAcountsForZone()
        {
            return QQZoneMarketingDao.QQAcountDao.GetEnabledAcountForZone();
        }
        /// <summary>
        /// 获取QQ微博启用了的账号
        /// </summary>
        /// <returns></returns>
        internal static List<QQAcount> GetEnabledQqAcountsForWb()
        {
            return QQWBDao.QQAcountDao.GetEnabledAcountForWB();
        }
        /// <summary>
        /// 设置QQ空间登录状态
        /// </summary>
        /// <param name="loginqq"></param>
        /// <returns></returns>
        internal static bool SetQQZoneLoginState(string loginqq)
        {
            var islogins=QQZoneMarketingDao.QQAcountDao.GetZoneIsLoginAcount();
            foreach (var islogin in islogins)
            {
                QQAcountDao.SetZoneLoginState(islogin.QQ, false);
            }
            QQAcountDao.SetZoneLoginState(loginqq, true);
            return true;
        }
        /// <summary>
        /// 设置QQ微博登录状态
        /// </summary>
        /// <param name="loginqq"></param>
        /// <returns></returns>
        internal static bool SetQQWbLoginState(string loginqq)
        {
            var islogins = QQWBDao.QQAcountDao.GetIsLoginAcountForWB();
            foreach (var islogin in islogins)
            {
                QQWBDao.QQAcountDao.SetLoginStateForWB(islogin.QQ, false);
            }
            QQWBDao.QQAcountDao.SetLoginStateForWB(loginqq, true);
            return true;
        }
    }
}
